package com.lishe.lishe.lishe.module.auth.fragment;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.module.auth.http.AuthHttpHelper;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.SharedPreferencesUtils;
import com.lishe.lishe.lishe.utils.ValidationUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by zhcase on 2017/9/5.
 */

public class RegEndFragment extends AuthBaseFragment implements View.OnClickListener{

    public static final String TAG = RegEndFragment.class.getSimpleName();

    private ViewPager viewPager = null;

    private EditText inputText1,inputText2;


    public RegEndFragment() {
    }

    public static RegEndFragment newInstance(){
        RegEndFragment instance = new RegEndFragment();
        Bundle bundle = new Bundle();
        instance.setArguments(bundle);
        return instance;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Log.e(TAG,"onCreate"+AuthHttpHelper.phoneOfReg);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {

        View containerFragment = inflater.inflate(R.layout.activity_auth_reg_end, container, false);

        ImageButton back = (ImageButton) containerFragment.findViewById(R.id.back_title);
        back.setOnClickListener(this);

        inputText1 = (EditText) containerFragment.findViewById(R.id.inputText1);

        inputText2 = (EditText) containerFragment.findViewById(R.id.inputText2);

        Button submitBtn = (Button) containerFragment.findViewById(R.id.submitBtn);
        submitBtn.setOnClickListener(this);

        TextView redirectBtn1 = (TextView) containerFragment.findViewById(R.id.redirectBtn1);
        redirectBtn1.setClickable(true);
        redirectBtn1.setOnClickListener(this);
        //Log.e(TAG,"onCreateView"+AuthHttpHelper.phoneOfReg);

        return containerFragment;
    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.back_title) {
//            if (viewPager != null) {
//                viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_Reg, true);
//            }

            if(mJumper!=null)
                mJumper.jumpToFragmentBack(FragmentAuthContants.TAB_INDEX_Reg);

        } else if (view.getId() == R.id.redirectBtn1) {

            //用户协议
            if (viewPager != null) {
                //viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_Reg, true);
            }


        } else if (view.getId() == R.id.submitBtn) {

            final String password1 = inputText1.getText().toString().trim();
            final String password2 = inputText2.getText().toString().trim();

            if(!ValidationUtils.isLimitPass(password1)||!ValidationUtils.isLimitPass(password2)){
                Toast.makeText(getActivity(), R.string.LimitPass, Toast.LENGTH_SHORT).show();
                return;
            }

            HashMap params = new HashMap();
            params.put("mobile",AuthHttpHelper.phoneOfReg);
            params.put("password",password1);
            params.put("affirmPassword",password2);

            LoadingUtils.createLoadingDialog(getActivity(),"请稍等...",true);
            AuthHttpHelper.doRegister(getActivity(),params, new LSWebHelper.WebCallBack() {
                @Override
                public void onSuccess(JSONObject dataJson, String dataString, String message) {
                    SharedPreferencesUtils.removeToken(getActivity());
                    SharedPreferencesUtils.removeUserId(getActivity());
                    AuthHttpHelper.phoneOfReg = null;
                    LoadingUtils.closeDialog();
                    inputText1.setText("");
                    inputText2.setText("");
                    getActivity().finish();
                }

                @Override
                public void onFailure(String message) {
                    LoadingUtils.closeDialog();
                    Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                }
            });


        }
    }
}
